﻿using System;
using ProdControls.Utility;
using ProdControls.Verification;
using ProdLogging;
using ProdUI.Hook.NativePatterns;

namespace ProdControls
{
    /// <summary>
    ///   Methods to work with Button controls using the UI Automation framework
    /// </summary>
    internal sealed class ProdTabNative
    {
        /// <summary>
        ///   Gets the tab count.
        /// </summary>
        /// <param name="windowHandle"> The window handle. </param>
        /// <returns> The number of tabs within the tabControl </returns>
        internal static int GetTabCount(IntPtr windowHandle)
        {
            LogController.ReceiveLogMessage(new LogMessage("Using SendMessage"));
            return (int) NativeMethods.SendMessage(windowHandle, (int) TabControlMessage.TCM_GETITEMCOUNT, 0, 0);
        }

        /// <summary>
        ///   Gets the selected tab.
        /// </summary>
        /// <param name="windowHandle"> The window handle. </param>
        /// <returns> The zero based index of the selected TabItem </returns>
        internal static int GetSelectedTab(IntPtr windowHandle)
        {
            LogController.ReceiveLogMessage(new LogMessage("Using SendMessage"));
            return (int) NativeMethods.SendMessage(windowHandle, (int) TabControlMessage.TCM_GETCURSEL, 0, 0);
        }

        /// <summary>
        ///   Sets the selected tab.
        /// </summary>
        /// <param name="windowHandle"> The window handle. </param>
        /// <param name="index"> The zero based index of the tab to select. </param>
        internal static void SetSelectedTab(IntPtr windowHandle, int index)
        {
            LogController.ReceiveLogMessage(new LogMessage("Using SendMessage"));
            NativeMethods.SendMessage(windowHandle, (int) TabControlMessage.TCM_SETCURSEL, index, 0);
            ValueVerifier<int, int>.Verify(index, GetSelectedTab(windowHandle));
        }
    }
}